/**
 * Created by tums on 18/8/15.
 */
import React from 'react';
import {createStore,applyMiddleware} from 'redux';
import {Provider} from 'react-redux';
import App from './App.js';
import todoApp from './reducers.js';
import {Promise} from 'es6-promise';

const logger = store=>next=>action=> {
    console.log('logger start',action);
    var result = next(action);
    console.log('logger end',action);
    return result;
};

const promise = store=>next=>action=> {
    console.log('Store 1:',store.dispatch.toString());
    if (typeof action.then !== 'function') {
        return next(action);
    }
    return Promise.resolve(action).then((r)=> {
        console.log('Store 2:',store.dispatch.toString());
        console.log('promise start',action);
        store.dispatch(r);
        console.log('promise end',action);
    });
};

let createMiddlewareStore = applyMiddleware(promise,logger)(createStore);

let store = createMiddlewareStore(todoApp);

React.render(
    <Provider store={store}>
        {()=><App/>}
    </Provider>,
    document.body
);